import urllib.request
import json
from html.parser import HTMLParser


def open_url(url):
    req = urllib.request.Request(url)
    req.add_header('User-Agent',
                   'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko')
    response = urllib.request.urlopen(req)
    return response.read().decode('utf-8')


url = 'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E5%8D%A2%E7%A1%95%E5%B2%A9&oq=%25E5%258D%25A2%25E7%25A1%2595%25E5%25B2%25A9&rsv_pq=c52904ac0003245c&rsv_t=9bb8CQDqJQkj0uPBmNO1JQHKQqNSoiqCc9SVLaMITHREaH9dCj405qETUgo&rqlang=cn&rsv_dl=tb&rsv_enter=0&rsv_sug=1'
html = open_url(url)
#print(html)

printTag = False
currentTitle = ''
currentLink = ''
index = 1


class BaiduParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        global currentTitle
        global currentLink
        global index
        if 'div' == tag and len(attrs) == 3:
            div = json.loads(attrs[2][1])
            if div.get('title'):
                print('----------- ' + str(index) + ' -----------')
                print('标题: ' + div['title'])
                print('链接: ' + div['url'])
                index = index + 1


parser = BaiduParser()

print('百度搜索“卢硕岩”结果的第一页')
parser.feed(html)
